﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VINETLIFE.BL;
using VINETLIFE.DO;
using System.IO;

namespace VINETLIFE.AdminCP
{
    public partial class Article : System.Web.UI.Page
    {
        cmsArticleDO objArt = new cmsArticleDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            //bindToDropDown(cboCategoryCate);
            Functions.ddlDatabinder(cboCategoryCate, cmsCategoryDO.CATEGORYID_FIELD, cmsCategoryDO.TITLE_FIELD, new cmsCategoryBL().SelectAdmin(false));
            if (Request.QueryString["ArticleID"] != null)
            {
                objArt.ArticleID = int.Parse(Request.QueryString["ArticleID"].ToString());

                if (!IsPostBack)
                {
                    initForm();
                    LoadFolder();
                }
            }
            else
            {
                if (!IsPostBack) { LoadFolder(); }
            }
        }
        protected void LoadFolder(
)
        {
            if (Session["UserID"] != null)
            {
                if (!Directory.Exists(Server.MapPath("~/Media/ArticleUpload/" + Session["UserID"].ToString() + "/")))
                    Directory.CreateDirectory(Server.MapPath("~/Media/ArticleUpload/" + Session["UserID"].ToString() + "/"));

                txtDetail.ImageManager.ViewPaths = txtDetail.ImageManager.DeletePaths = new string[] { "~/Media/ArticleUpload/" + Session["UserID"].ToString() + "/" };
                txtDetail.ImageManager.UploadPaths = new string[] { "~/Media/ArticleUpload/" + Session["UserID"].ToString() + "/" };
                txtDetail.ImageManager.DeletePaths = new string[] { "~/Media/ArticleUpload/" + Session["UserID"].ToString() + "/" };
            }
        }
        private void initForm()
        {
            hplImage.Visible = true;
            objArt = new cmsArticleBL().Select(objArt);
            txtTitle.Text = objArt.Title;
            txtDescription.Text = objArt.Description;
            //  txtArticleDetail.Text = objArt.ArticleDetail;
            txtDetail.Content = objArt.ArticleDetail;
            chkIsHomePage.Checked = objArt.IsHompage;
            chkIsPublish.Checked = !objArt.IsPublish;
            txtOrderID.Text = objArt.OrderID.ToString();
            cboCategoryCate.SelectedValue = objArt.CategoryID.ToString();
            hplImage.NavigateUrl = "~/Media/" + objArt.ImageUrl;
            chkVote.Checked = objArt.AllowVote;
            chkComment.Checked = objArt.AllowComment;
            txtMetaDes.Text = objArt.MetaDes;
            txtMetaTag.Text = objArt.MetaTag;
            txtPageTitle.Text = objArt.PageTitle;

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            initObject();
            if (objArt.ArticleID <= 0)
            {
                objArt.CreateDate = DateTime.Now;
                objArt.UserCreate = 1;// int.Parse(Session["UserID"].ToString());
                new cmsArticleBL().Insert(objArt);
            }
            else
            {
                new cmsArticleBL().Update(objArt);
            }
            Functions.Alert("Cập nhật thành công!", "ListArticle.aspx");
        }
        private void initObject()
        {
            objArt = new cmsArticleBL().Select(objArt);
            objArt.Title = txtTitle.Text;
            objArt.Description = txtDescription.Text;
            //objArt.ArticleDetail = txtArticleDetail.Text;
            objArt.ArticleDetail = txtDetail.Content;
            objArt.IsHompage = chkIsHomePage.Checked;
            objArt.IsPublish = !chkIsPublish.Checked;
            objArt.OrderID = int.Parse(txtOrderID.Text);
            objArt.CategoryID = int.Parse(cboCategoryCate.SelectedValue);
            objArt.AllowVote = chkVote.Checked;
            objArt.AllowComment = chkComment.Checked;
            objArt.PageTitle = txtPageTitle.Text;
            objArt.MetaTag = txtMetaTag.Text;
            objArt.MetaDes = txtMetaDes.Text;

            if (!string.IsNullOrEmpty(fuImage.FileName))
                objArt.ImageUrl = UploadFile(fuImage);
        }

        private string UploadFile(FileUpload fulImage)
        {
            if (!string.IsNullOrEmpty(fulImage.FileName))
            {
                string FileName = string.Format("{0}{1}", Functions.Change_AV(txtTitle.Text) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"), fulImage.FileName.Substring(fulImage.FileName.LastIndexOf(".")));
                FileName = FileName.Replace('\u0111', 'd').Replace('\u0110', 'D').Replace(" ", "-").Replace("/", "-").Replace("?", "-").Replace("\\", "-").Replace("'", "-").Replace(":", "-").Replace("\"", "-");
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                fulImage.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }
        public void bindToDropDown(DropDownList ddl)
        {
            DataTable dtCate = new cmsCategoryBL().SelectAdmin(false); //new DataView(new cmsMenuBL().SelectAll(),"IsContent = 0","",DataViewRowState.CurrentRows).ToTable();
            foreach (DataRow dr in dtCate.Rows)
            {
                if (dr["ParentID"].ToString().Equals("0"))
                {
                    ListItem item = new ListItem();
                    item.Value = dr["CategoryID"].ToString();
                    //item.Text = "+ " + dr["Title"].ToString();
                    ddl.Items.Add(item);
                    GetChildItems(dr["CategoryID"].ToString(), dtCate, ddl);
                }
            }
        }
        private void GetChildItems(string parentID, DataTable dtTemp, DropDownList ddl)
        {
            foreach (DataRow dr in dtTemp.Rows)
            {
                if (dr["ParentID"].ToString().Equals(parentID))
                {
                    ListItem listChild = new ListItem();
                    listChild.Text = "-------" + dr["Title"].ToString();
                    listChild.Value = dr["CategoryID"].ToString();
                    listChild.Attributes.Add("style", "background:#f9f9f9;color:#648293;");
                    ddl.Items.Add(listChild);
                    //       GetChildItems(dr["MenuID"].ToString(), dtTemp, ddl);
                }
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("/AdminCP/ListArticle.aspx");
        }
    }
}